import base64
import zlib
from parsers import lxml_target
from PyQt4 import QtGui, QtCore
import sys

class createXMLFile:

	def __init__(self, xmlfile, listchroma, header):
		self.xmlfile = xmlfile
		self.listchroma =listchroma
		self.header =header

	def writing_xml(self):
		f=open(self.xmlfile+".txt",'w')
		f.write(self.header)
		for i, spectra in enumerate(self.listchroma):
			f.write("<scan num=\""+str(i+1)+"\"\n")
			f.write("msLevel=\"1\"\n")
			f.write("peaksCount=\""+str(len(spectra))+"\"\n")
			f.write("retentionTime=\"PT"+str(spectra.t_min)+"S\">\n")
			f.write("<peaks precision=\"32\"\n")
			f.write("byteOrder=\"network\"\n")
			f.write("pairOrder=\"m/z-int\">"+str(spectra.encode64())+"</peaks>\n")
			f.write("</scan>\n")
			f.write("</msRun>\n")
			f.write("</mzXML>")   
   
	def get_new_file(self):
		return self.xmlfile+".txt"

if __name__=="__main__":
	app = QtGui.QApplication(sys.argv)
	parser= lxml_target.lxml_parser("output-s6.mzXML",3.03)
	parser.parsing()
	writer= createXMLFile(parser)
	writer.writing_xml()
	app.exec_()
